<?php

namespace app\api\controller;

use app\api\model\Third;
use app\api\model\User as UserM;
use app\common\trilateral\wxBizDataCrypt;
use app\common\controller\Api;

/**
 * 登录接口
 */
class Login extends Api
{
    protected $noNeedLogin = ['wx'];
    protected $noNeedRight = ['*'];

    //微信登录
    public function wx()
        {
            $code = request()->param('code');
            $appid = config('site.wx_appid');
            $secret = config('site.wx_secret');
            $wxUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
            $getUrl = sprintf($wxUrl, $appid, $secret, $code);
            $wx_data = $this->code_curl($getUrl);
    //        $wx_data['openid']=$code;
            if (empty($wx_data['errcode'])) {
                $Third = new Third();
                $UserM = new UserM();
                $where_Third['openid'] = $wx_data['openid'];
                $Third_find = $Third->get($where_Third);
                if (empty($Third_find)) {
                    $UserM->nickname = "";
                    $UserM->avatar = "";
                    $UserM->status = "normal";
                    $UserM->jointime = time();
                    $UserM->joinip = request()->ip();
                    $UserM->save();
                    $Third->user_id = $UserM->id;
                    $Third->platform = "微信";
                    $Third->apptype = "小程序";
                    $Third->unionid = $wx_data['openid'];
                    $Third->openid = $wx_data['openid'];
                    $Third->save();
                    $user_id = $UserM->id;
                    $is_mobile = 0;
                } else {
                    $user_id = $Third_find['user_id'];
                    $user_find = $UserM->get($Third_find['user_id']);
                    if (empty($user_find['mobile'])) {
                        $is_mobile = 0;
                    } else {
                        $is_mobile = 1;
                    }
                }
                $this->auth->direct($user_id);
                $data['token'] = $this->auth->getToken();
                $data['openid'] = $wx_data['openid'];
                $data['sessionKey'] = $wx_data['session_key'];
                $data['is_mobile'] = $is_mobile;
            } else {
                $this->error('错误', $wx_data['errmsg']);
            }
            $this->success('', $data);
        }


    //获取手机号
    public function wxGetPhone()
    {
        $param = request()->param();
        $user = $this->auth;
        $UserM = new UserM();
        $appid = config('site.wx_appid');//小程序的appid
        $sessionKey = $param['sessionKey'];//小程序传传过来的session_key
        $encryptedData = $param['encryptedData'];//小程序传传过来的encryptedData
        $iv = $param['iv'];//小程序传传过来的iv
        if (empty($sessionKey)) {
            $this->error('sessionKey不能为空');
        }
        if (empty($encryptedData)) {
            $this->error('encryptedData不能为空');
        }
        if (empty($iv)) {
            $this->error('iv不能为空');
        }
        $wx = new WXBizDataCrypt($appid, $sessionKey);
        $errCode = $wx->decryptData($encryptedData, $iv, $datase);
        if ($errCode == 0) {
            $data = json_decode($datase, true);
            $updata_user['mobile'] = $data['phoneNumber'];
            $where['id'] = $user->id;
            $UserM->save($updata_user, $where);
            $this->success('绑定成功', $data);
        } else {
            $this->error('sessionKey过期请重新授权登录');
        }


    }

    //请求微信方法
    public function code_curl($url)
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_SSL_VERIFYPEER => false,
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return json_decode($response, true);
    }
}
